﻿Public Class cls_SalesDetail_Update
    Inherits cls_Database_Base
#Region "Get Data"
    Public Function FuncG_GetData() As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT * FROM [TBL_SALES_DETAIL]"
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
    Public Function FuncG_GetData(ByVal iStr_ID As String) As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT TBL_SALES_DETAIL.ITEM_ID, TBL_ITEM.NAME AS [Tên hàng], TBL_SALES_DETAIL.QUANTITY AS [S.L], TBL_SALES_DETAIL.PRICE AS [Giá],"
                mvStr_Sql += "  TBL_SALES_DETAIL.DISCOUNT, TBL_SALES_DETAIL.SERVICE, TBL_SALES_DETAIL.VAT"
                mvStr_Sql += "  FROM TBL_SALES_DETAIL INNER JOIN TBL_ITEM ON TBL_SALES_DETAIL.LOC_ID = TBL_ITEM.LOC_ID AND TBL_SALES_DETAIL.ITEM_ID = TBL_ITEM.ID"
                mvStr_Sql += "  WHERE TBL_SALES_DETAIL.LOC_ID = @iNVChr_LOCID AND TBL_SALES_DETAIL.[ID] = @iNVChr_ID"
                mvStr_Sql += "  ORDER BY TBL_SALES_DETAIL.CREATEDATE"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
    Public Function FuncG_GetTotal(ByVal iStr_ID As String) As DataTable
        Dim mvDataTale As New DataTable
        Dim mvStr_Sql As String = ""
        Try
            If FuncM_OpenConnection() Then
                mvStr_Sql = "SELECT SUM(QUANTITY * PRICE) AS TOTAL,"
                mvStr_Sql += "  SUM(QUANTITY * PRICE * DISCOUNT /100) AS DISCOUNT,"
                mvStr_Sql += "  SUM(QUANTITY * PRICE * SERVICE /100) AS SERVICE,"
                mvStr_Sql += "  SUM(QUANTITY * PRICE * VAT /100) AS VAT,"
                mvStr_Sql += "  SUM(QUANTITY * PRICE + (QUANTITY * PRICE * (VAT + SERVICE - DISCOUNT) /100)) AS TOTALCOST"
                mvStr_Sql += "  FROM TBL_SALES_DETAIL WHERE LOC_ID = @iNVChr_LOCID AND [ID] = @iNVChr_ID"
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iStr_ID)
                mvDataTale = FuncM_GetDataTable(mvStr_Sql)
            End If
        Catch ex As Exception
            Throw ex
        Finally
            FuncM_CloseConnection()
        End Try
        Return mvDataTale
    End Function
#End Region
#Region "Insert - Update"
    Public Function FuncG_InsertUpdate(ByVal iCls_SalesDetail As cls_SalesDetail) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_SalesDetail.ID)
                SubM_AddParameter("@iInt_ITEM", iCls_SalesDetail.ItemID)
                SubM_AddParameter("@iFlt_QUANTITY", iCls_SalesDetail.Quantity)
                SubM_AddParameter("@iFlt_PRICE", iCls_SalesDetail.Price)
                SubM_AddParameter("@iNVChr_NOTE", iCls_SalesDetail.Note)
                If FuncM_ExecuteStore("usp_SalesDetail") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try

    End Function
    'Cập nhật lại số lượng khi trả hàng
    Public Function FuncG_Update(ByVal iCls_SalesDetail As cls_SalesDetail) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_SalesDetail.ID)
                SubM_AddParameter("@iInt_ITEM", iCls_SalesDetail.ItemID)
                SubM_AddParameter("@iFlt_QUANTITY", iCls_SalesDetail.Quantity)
                If FuncM_ExecuteStore("usp_UpdateSalesDetail") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try

    End Function
    Public Function FuncG_UpdateNote(ByVal iCls_SalesDetail As cls_SalesDetail) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_SalesDetail.ID)
                SubM_AddParameter("@iInt_ITEM", iCls_SalesDetail.ItemID)
                SubM_AddParameter("@iNVChr_NOTE", iCls_SalesDetail.Note)
                If FuncM_ExecuteStore("usp_UpdateNote") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try

    End Function
#End Region
#Region "Update Fee"
    Public Function FuncG_UpdateFee(ByVal iCls_SalesDetail As cls_SalesDetail, ByVal iByte_Status As Byte) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_SalesDetail.ID)
                SubM_AddParameter("@iInt_ITEM", iCls_SalesDetail.ItemID)
                SubM_AddParameter("@iSInt_VALUE", iCls_SalesDetail.Price)
                SubM_AddParameter("@iTInt_STATUS", iByte_Status)
                If FuncM_ExecuteStore("usp_UpdateFee") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
    Public Function FuncG_UpdateFeeBill(ByVal iCls_SalesDetail As cls_SalesDetail, ByVal iByte_Status As Byte) As Boolean
        Try
            If FuncM_OpenConnection() Then
                SubM_AddParameter("@iNVChr_LOCID", gvStr_LocID)
                SubM_AddParameter("@iNVChr_ID", iCls_SalesDetail.ID)
                SubM_AddParameter("@iSInt_VALUE", iCls_SalesDetail.Price)
                SubM_AddParameter("@iTInt_STATUS", iByte_Status)
                If FuncM_ExecuteStore("usp_UpdateFeeBill") Then
                    Return True
                Else
                    Return False
                End If
            End If
        Catch ex As Exception
            Throw ex
            Return False
        Finally
            FuncM_CloseConnection()
        End Try
    End Function
#End Region
End Class
